### yianni's other question ###

from worker import Worker
import matplotlib
matplotlib.use('Agg')
from pylab import *
#from numpy import *

class Question3(Worker):
    """ Plot the yearly average metascore for movies produced from 2000 to 2007. (Use the Large Data Set) """
    def __init__(self, framework):
        Worker.__init__(self, framework)
        self.title = 'Question 3'
        
    def get_result(self):
        ds = self.framework.large_dataset
        dict = {} # dict to store {years:avg metascore}
        years = range(2000, 2008) # list of all possible years
        num = 0

        # convert year ints into strs
        for year in years: 
            years[num] = str(years[num])
            num += 1

        # for each year, cycle through each record, if year matches then add its metscore/count+1
        for year in years:
            metascore_total = 0
            count = 0
            for record in ds:
                if record[9] == year:
                    metascore = int(record[25])
                    metascore_total += metascore
                    count += 1
            # calc the average metascore for the year, store in dict
            dict[year] = metascore_total / count

        # plot data with matplotlib module
        bar(range(0, len(dict.keys())), dict.values())
        title('Annual Average Metascore')
        xlabel('Year')
        ylabel('Average Metascore')
        #ind = np.arange(N)    # the x locations for the groups
        #xticks(ind+width/2., dict.keys())
        #yticks(np.arange(0, max(dict.values()), max(dict.values())/10))
        savefig('q3.png')
        
        #fig = gcf() # get current figure
    #return ('This graph shows the yearly average metascore for movies from 2000-2007',fig)
